package com.lh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.RectificationInfo;
import com.lh.entity.table.ReminderInfo;
import com.lh.entity.table.UpdateProgress;
import com.lh.mapper.RectificationInfoMapper;
import com.lh.mapper.UpdateProgressMapper;
import com.lh.service.UpdateProgressService;
import com.lh.utils.Answer;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UpdateProgressServiceImpl implements UpdateProgressService {
    @Resource
    private UpdateProgressMapper updateProgressMapper;
    @Resource
    private RectificationInfoMapper rectificationInfoMapper;


    @Override
    public Answer<Object> getUpdateProgressByRectificationId(JSONObject reqJson) {
        String rectificationId = reqJson.getString("rectificationId");
        List<UpdateProgress> updateProgresses = updateProgressMapper.selectList(new LambdaQueryWrapper<UpdateProgress>()
                .eq(UpdateProgress::getRectificationId, rectificationId).orderByDesc(UpdateProgress::getId));
        UpdateProgress updateProgress = updateProgresses.get(0);
        return Answer.ok(updateProgress);
    }

    @Override
        public Answer<Object> insertUpdateProgress(UpdateProgress updateProgress) {
        updateProgressMapper.insert(updateProgress);
        return Answer.ok("添加成功");
    }

//    @Override
//    public Answer<Object> getTodayUpdateProgress(JSONObject reqJson) {
//        String date = reqJson.getString("date");
//
//        LocalDateTime startTime = null;
//        LocalDateTime endTime = null;
//
//        // 去掉日期验证：直接尝试转换，转换失败则不限制日期（查询所有）
//        if (date != null && !date.trim().isEmpty()) {
//            try {
//                LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE);
//                startTime = localDate.atStartOfDay();
//                endTime = localDate.atTime(LocalTime.MAX);
//            } catch (Exception e) {
//                // 日期格式错误时，不限制日期范围（查询所有type=2的记录）
//                startTime = null;
//                endTime = null;
//            }
//        }
//        List<UpdateProgress> updateProgresses = updateProgressMapper.selectList(new LambdaQueryWrapper<UpdateProgress>().ge(startTime != null, UpdateProgress::getCreateTime, startTime)
//                .le(endTime != null, UpdateProgress::getCreateTime, endTime));
//
//        List<RectificationInfo> rectificationInfoList = rectificationInfoMapper.selectList(new LambdaQueryWrapper<>());
//
//        Map<Integer, RectificationInfo> rectificationMap = rectificationInfoList.stream()
//                .collect(Collectors.toMap(
//                        RectificationInfo::getId,
//                        info -> info,
//                        (oldValue, newValue) -> oldValue
//                ));
//
//        return null;
//    }
}
